package com.xywy.beautyand.ACT;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.beautyand.R;
import com.xywy.beautyand.ADAPTER.QuestionDetailAdapter;
import com.xywy.beautyand.base.BaseActivity;
import com.xywy.beautyand.bean.QuestionReplay;
import com.xywy.beautyand.custom.MyListView;
import com.xywy.beautyand.util.Constants;
import com.xywy.beautyand.util.JsonUtil;
import com.xywy.beautyand.util.UIUtil;
import com.xywy.beautyand.vollery.GetRequest;
import com.xywy.beautyand.vollery.OnVolleyResponseListener;

/**
 * *****************************************
 * 
 * @Title: QuestionDetail.java
 * @Package com.xywy.performgirlsandroid.act
 * @Description: TODO(问题详情页)
 * @author smz
 * @date 2015-1-4 上午9:45:45
 * @version V1.0
 ******************************************* 
 */
public class QuestionDetailAct extends BaseActivity {

	private RelativeLayout left_img;
	private MyListView mylistview;
	private List<QuestionReplay> list;
	private QuestionDetailAdapter adapter;
	private TextView title;
	private ImageView sex_img;
	private TextView sex_text;
	private TextView date_time;
	private TextView question_detail;
	
	private static final int SUCESS_FUL=1;

	private String Title;
	private  String Detail;
	private String Sex;
	private String Age;
	private String AskTime;
	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_question_detail);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		TextView middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(R.string.question_detail);
		list = new ArrayList<QuestionReplay>();
//		for (int i = 0; i < 5; i++) {
//			list.add("你好");
//		}
		mylistview = (MyListView) findViewById(R.id.my_listview);
		adapter = new QuestionDetailAdapter(context, list);
		mylistview.setAdapter(adapter);
		
		
		
		title = (TextView) findViewById(R.id.title);
		sex_img = (ImageView) findViewById(R.id.sex_img);
		sex_text = (TextView) findViewById(R.id.sex_text);
		date_time = (TextView) findViewById(R.id.date_time);
		question_detail = (TextView) findViewById(R.id.question_detail);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
	}

	@Override
	protected void initData() {
     String id=getIntent().getExtras().getString("id", "1314");
     getQuestiionDetail(id);
	}
  /**
   * 获取问题的详情
   */
	private void getQuestiionDetail(String id) {
		GetRequest<String> getRequest=new GetRequest<String>(Constants.getUrl("club", "Club_quesDetail")+"&art_id="+id, String.class, new OnVolleyResponseListener<String>() {

			@Override
			public void onError(VolleyError volleyError) {
				UIUtil.DissDialog(context, waittingDialog);
				UIUtil.showToast(context, context.getResources().getString(R.string.network_slow));
			}

			@Override
			public void onSuccess(String response) {
				UIUtil.DissDialog(context, waittingDialog);
				try {
					JSONObject object=new JSONObject(response);
					int state=object.optInt("state");
					if(state==200){
						JSONObject object1=object.optJSONObject("data");
						 Title=object1.optString("title");
						 Detail=object1.optString("detail");
						 Sex=object1.optString("sex");
						 Age=object1.optString("age");
						 AskTime=object1.optString("askTime");
						String repliedList=object1.optString("repliedList");
						
						
						List<QuestionReplay> tempData = (List<QuestionReplay>) JsonUtil
								.parseJsonToList(
										repliedList,
										new TypeToken<List<QuestionReplay>>() {
										}.getType());
						list.addAll(tempData);
						Message message = handler.obtainMessage();
						message.what = SUCESS_FUL;
						handler.sendMessage(message);
					}else{
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
		getRequest.setIsParseJson(false);
		executeRequest(getRequest);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCESS_FUL:
//			title = (TextView) findViewById(R.id.title);
//				sex_img = (ImageView) findViewById(R.id.sex_img);
//				sex_text = (TextView) findViewById(R.id.sex_text);
//				date_time = (TextView) findViewById(R.id.date_time);
//				question_detail
				
				title.setText(Title);
				if("男".equals(Sex)){
					sex_img.setBackgroundResource(R.drawable.man_img);
					sex_text.setText(Sex);
				}
				date_time.setText(AskTime);
				
				question_detail.setText(Detail);
				adapter.notifyDataSetChanged();
				((ScrollView) findViewById(R.id.scrollview)).smoothScrollTo(0, 20);
				break;
			}
		}
	};

}
